package com.pxene.dmp.mapper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import com.pxene.dmp.common.URLUtils;
import com.pxene.dmp.constant.PatternList;
import com.pxene.dmp.constant.PatternList.Param;
import com.pxene.dmp.constant.PatternList.Pattern;

public class GetAutoInfoMapper extends Mapper<LongWritable, Text, Text, Text> {
	private Log log = LogFactory.getLog(GetAutoInfoMapper.class);
	private URL urlParser;
	private Text resultKey = new Text();
	private Text resultVal = new Text();

	@Override
	protected void setup(Context context) throws IOException, InterruptedException {
		Configuration conf = context.getConfiguration();
		// 设置配置文件名
		System.setProperty("config-file", conf.get("config-file"));
	}

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String[] columns = value.toString().split("\\|");
		if (columns.length != 7) {
			return;
		}
		String imsi = columns[0];
		String bsid = columns[1];
		String starttime = columns[2];
		String domain = columns[3];
		String url = columns[4];
		String province = columns[5];
		String dataType = columns[6];

		try {
			urlParser = new URL(url);
		} catch (MalformedURLException e) {
			return;
		}

		String city = "";
		String auto = "";

		PatternList autoInfoList = PatternList.load("autoinfo.json");
		for (Pattern autoInfo : autoInfoList.elements()) {
			if (autoInfo.getCode().equals(domain)) {
				Param[] params = autoInfo.getParams();

				for (int i = 0; i < params.length; i++) {
					if ("city".equals(params[i].getId())) {
						String expr0 = params[i].getExpr();
						auto = URLUtils.regexpExtract(url, expr0).trim();
					}
					if ("auto".equals(params[i].getId())) {
						String expr0 = params[i].getExpr();
						auto = URLUtils.regexpExtract(url, expr0).trim();
					}
				}
			}
		}
		log.info("AAAA" + city + "-----" + auto); // 日志打印
		if (auto.length() > 0) {
			resultKey.set(imsi + "|" + bsid + "|" + starttime + "|" + domain + "|" + city + "|" + province + "|" + dataType);
			resultVal.set(auto);
			context.write(resultKey, resultVal);
		}
	}

//	public static void main(String[] args) {
//		String url = "http://m.ycapp.huimaiche.com/order1?carid=117095";
//		String city = "";
//		String auto = "";
//
//		PatternList autoInfoList = PatternList.load("autoinfo.json");
//		for (Pattern autoInfo : autoInfoList.elements()) {
//			// System.out.println(autoInfo.getCode());
//			if (autoInfo.getCode().equals("00030007002")) {
//
//				Param[] params = autoInfo.getParams();
//
//				for (int i = 0; i < params.length; i++) {
//					if ("city".equals(params[i].getId())) {
//						// System.out.println(params[i].getExpr());
//						String expr0 = params[i].getExpr();
//						auto = URLUtils.regexpExtract(url, expr0).trim();
//					}
//					if ("auto".equals(params[i].getId())) {
//						// System.out.println(params[0].getExpr());
//						String expr0 = params[i].getExpr();
//						auto = URLUtils.regexpExtract(url, expr0).trim();
//					}
//				}
//			}
//		}
//		System.out.println(city);
//		System.out.println(auto);
//	}
}
